//
//  HWVipGiftBagCell.swift
//  HWanGame
//
//  Created by 王昌健 on 2021/12/10.
//  Copyright © 2021 94Wan. All rights reserved.
//

import UIKit

class HWVipGiftBagCell: UITableViewCell {
    
    var titleLabel:UILabel!
    var contentLabel:UILabel!
    var receiveButton:UIButton!
    var conditionLabel:UILabel!
    var lineImageView:UIImageView!

    var receiveButtonClick:((_:String)->())?
    
    var model:HWVipGiftBagListModel? {
        didSet {
            titleLabel.text = model?.cardname
            contentLabel.text = model?.content
            conditionLabel.text = model?.title
            receiveButton.isEnabled = true
            if model?.status == 0 {
                receiveButton.setTitle("领取", for: .normal)
                receiveButton.setTitleColor(UIColor.colorWithHexString(hexString: "#4F3200"), for: .normal)
                receiveButton.setBackgroundImage(UIImage(named: "wearButton"), for: .normal)
            }else if model?.status == 1 {
                receiveButton.setTitle("已领取", for: .normal)
                receiveButton.setTitleColor(UIColor.colorWithHexString(hexString: "#666666"), for: .normal)
                receiveButton.setBackgroundImage(UIImage(named: "receiveButtonGray"), for: .normal)
            }else if model?.status == 2 {
                receiveButton.setTitle("领取", for: .normal)
                receiveButton.setTitleColor(UIColor.colorWithHexString(hexString: "#666666"), for: .normal)
                receiveButton.setBackgroundImage(UIImage(named: "receiveButtonGray"), for: .normal)
                receiveButton.isEnabled = false
            }
        }
    }
    class func cellWithTableView(tableView:UITableView) -> (HWVipGiftBagCell) {
        let reUse:String = "HWVipGiftBagCell"
        
        var cell = tableView.dequeueReusableCell(withIdentifier: reUse)
        
        if cell == nil {
            cell = HWVipGiftBagCell.init(style: UITableViewCell.CellStyle.default, reuseIdentifier: reUse)
        }
        
        return cell as! HWVipGiftBagCell
    }
    
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        initUI()
        initFrame()
    }
    override var frame: CGRect {
        get {
            return super.frame
        }
        set {
            var frame = newValue
            frame.origin.x += 15
            frame.size.width -= 2 * 15
            super.frame = frame
        }
    }
    func initUI() -> Void {
        selectionStyle = UITableViewCell.SelectionStyle.none
        backgroundColor = UIColor.colorWithHexString(hexString: "#F3F4F8")
//        contentView.backgroundColor = .red
        titleLabel = HWLabel.createBoldLabel(title: "-", color: "#333333", font: 12)
        contentView.addSubview(titleLabel)
        
        contentLabel = HWLabel.createLabel(title: "-", color: "#666666", font: 10)
        contentView.addSubview(contentLabel)
        
        receiveButton = UIButton(type: .custom)
        receiveButton.setTitle("领取", for: .normal)
        receiveButton.titleLabel?.font = .boldSystemFont(ofSize: 12)
        receiveButton.setTitleColor(.colorWithHexString(hexString: "#4F3200"), for: .normal)
        receiveButton.setBackgroundImage(UIImage(named: "wearButton"), for: .normal)
        receiveButton.addTarget(self, action: #selector(receiveButtonClick(_:)), for: .touchUpInside)
        contentView.addSubview(receiveButton)
        
        conditionLabel = HWLabel.createLabel(title: "-", color: "#999999", font: 10)
        contentView.addSubview(conditionLabel)
        
        lineImageView = UIImageView()
        lineImageView.backgroundColor = .colorWithHexString(hexString: "#CFCFCF")
        contentView.addSubview(lineImageView)
    }
    
    func initFrame() -> Void {
        titleLabel.snp.makeConstraints { make in
            make.top.equalTo(15)
            make.left.equalTo(12)
        }
        
        receiveButton.snp.makeConstraints { make in
            make.right.equalTo(-13)
            make.top.equalTo(15)
            make.width.equalTo(50)
            make.height.equalTo(27)
        }
        
        conditionLabel.snp.makeConstraints { make in
            make.centerX.equalTo(receiveButton)
            make.top.equalTo(receiveButton.snp.bottom).offset(5)
        }
        
        contentLabel.snp.makeConstraints { make in
            make.left.equalTo(12)
            make.top.equalTo(titleLabel.snp.bottom).offset(4)
            make.right.equalTo(receiveButton.snp.left).offset(-40)
            make.height.greaterThanOrEqualTo(30)
        }
        
        lineImageView.snp.makeConstraints { make in
            make.top.equalTo(contentLabel.snp.bottom).offset(10)
            make.bottom.equalToSuperview().offset(0)
            make.height.equalTo(1)
            make.left.equalTo(11)
            make.right.equalTo(-12)
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}

extension HWVipGiftBagCell {
    @objc func receiveButtonClick(_ button:UIButton) -> Void {
        self.receiveButtonClick?(self.model?.id ?? "")
    }
}
